package com.pro_test.proservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pro_test.commonutils.R;
import com.pro_test.proservice.entity.ProCharge;
import com.pro_test.proservice.entity.ProPark;
import com.pro_test.proservice.entity.vo.ChargeQuery;
import com.pro_test.proservice.service.ProChargeService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Captain
 * @since 2022-02-26
 */
@RestController
@RequestMapping("/proservice/pro-charge")
@CrossOrigin
public class ProChargeController {

    @Autowired
    private ProChargeService proChargeService;

    @ApiOperation(value = "查询所有收费")
    @GetMapping("findAll")
    public R findAllCharge()
    {
        List<ProCharge> list = proChargeService.list(null);
        return R.ok().data("items",list);
    }

    @ApiOperation(value = "逻辑删除收费")
    @DeleteMapping("{id}")
    public R removeCharge(@ApiParam(name = "id", value = "收费ID", required = true)
                          @PathVariable String id)
    {
        boolean flag = proChargeService.removeById(id);
        if (flag)
        {
            return R.ok();
        }
        else
        {
            return R.error();
        }
    }

    //条件查询分页
    @ApiOperation(value = "条件分页查询")
    @PostMapping("pageChargeCondition/{current}/{limit}")
    public R pageChargeCondition(@PathVariable long current,
                                 @PathVariable long limit,
                                 @RequestBody(required = false) ChargeQuery chargeQuery)
    {
        //创建page对象
        Page<ProCharge> pageCharge = new Page<>(current,limit);

        //构建条件
        QueryWrapper<ProCharge> wrapper = new QueryWrapper<>();
        Integer status = chargeQuery.getStatus();
        String begin = chargeQuery.getBegin();
        String end = chargeQuery.getEnd();


        if (!StringUtils.isEmpty(status))
        {
            wrapper.eq("status",status);
        }
        if (!StringUtils.isEmpty(begin))
        {
            wrapper.ge("gmt_create",begin);
        }
        if (!StringUtils.isEmpty(end))
        {
            wrapper.le("gmt_create",end);
        }
        //排序
        wrapper.orderByDesc("gmt_create");

        //调用方法实现条件查询分页
        proChargeService.page(pageCharge,wrapper);
        long total = pageCharge.getTotal();
        List<ProCharge> records = pageCharge.getRecords();

        return R.ok().data("total",total).data("items",records);
    }

    @ApiOperation(value = "添加收费")
    @PostMapping("addCharge")
    public R addCharge(@RequestBody ProCharge proCharge)
    {
        boolean save = proChargeService.save(proCharge);
        if (save)
        {
            return R.ok();
        }
        else
        {
            return R.error();
        }
    }

    @ApiOperation(value = "查询收费")
    @GetMapping("getCharge/{id}")
    public R getCharge(@PathVariable String id)
    {
        ProCharge proCharge = proChargeService.getById(id);
        return R.ok().data("charge",proCharge);
    }

    @GetMapping("getChargeByOwnerId/{ownerId}")
    public R getChargeByOwnerId(@PathVariable String ownerId)
    {
        Page<ProCharge> pageCharge = new Page<>();

        QueryWrapper<ProCharge> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(ownerId))
        {
            wrapper.eq("owner_id",ownerId);
        }
        proChargeService.page(pageCharge,wrapper);
        long total = pageCharge.getTotal();
        List<ProCharge> records = pageCharge.getRecords();

        return R.ok().data("total",total).data("proCharge",records);
    }

    @ApiOperation(value = "修改收费")
    @PostMapping("updateCharge")
    public R updateCharge(@RequestBody ProCharge proCharge)
    {
        boolean flag = proChargeService.updateById(proCharge);
        if (flag)
        {
            return R.ok();
        }
        else
        {
            return R.error();
        }
    }
}

